*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Figure VI									   *
********************************************************************************

cap log close      
log using ${log}/FigureVIabcd.log, replace       

* EUE movers 
use "$data/FTp_u__finaldata.dta", clear



*** variable cleaning for these analyses

* winsorizing 'coworker wage changes' variable
cap drop actual_ln_mover_change 
winsor mean_delta_ln_wage_mover_2015, gen(actual_ln_mover_change) p(0.02) 

* looping over the list of behaviors
foreach behavior in switchout_proba negotiate_proba negotiate_pct_c  paycut_quit_pct {
		
	* setting y-axis titles and filename suffixes						
	if "`behavior'"=="switchout_proba" { // Figure VI a
		local ytitle "Probability of Seeking New Job"
		local behsuffix "search"
	}
	if "`behavior'"=="paycut_quit_pct" { // Figure VI b
		local ytitle "Reservation Wage Cut"
		local behsuffix "rescut"
	}	
	if "`behavior'"=="negotiate_proba" { // Figure VI c
		local ytitle "Probability of Negotiating for Wage Raise"
		local behsuffix "bargain"
	}
	if "`behavior'"=="negotiate_pct_c" { // Figure VI d
		local ytitle "Wage Raise Magnitude"
		local behsuffix "bmagc"
	}	

	local xtitle "Belief About Wage Change if Moving to Outside Option"
						
	* calculating coefficients
	reg `behavior' ln_own_change_w if actual_ln_mover_change!=., cluster(pid)
		local beh_beta1 = string(round(_b[ln_own_change_w],0.001),"%4.3f")
		local beh_se1 = string(round(_se[ln_own_change_w],0.001),"%4.3f")
		local beh_constant1 = string(round(_b[_cons],0.001),"%4.3f")
		local beh_N1 = e(N)
				
	reg `behavior' ln_own_change_w actual_ln_mover_change if actual_ln_mover_change!=., cluster(pid)
		local beh_beta2 = string(round(_b[ln_own_change_w],0.001),"%4.3f")
		local beh_se2 = string(round(_se[ln_own_change_w],0.001),"%4.3f")
		local beh_constant2 = string(round(_b[_cons],0.001),"%4.3f")
		local beh_N2 = e(N)
		
	* partialling out the control variable(s), to prepare for plotting
	reg `behavior' actual_ln_mover_change if actual_ln_mover_change!=.
		predict r_`behavior' if actual_ln_mover_change!=., resid
		quietly su `behavior'
		replace r_`behavior' = r_`behavior'+r(mean)
				
	reg ln_own_change_w actual_ln_mover_change if actual_ln_mover_change!=.
		predict r_ln_own_change_w if actual_ln_mover_change!=., resid
		quietly su ln_own_change_w
		replace r_ln_own_change_w = r_ln_own_change_w+r(mean)
				
	reg r_`behavior' r_ln_own_change_w if actual_ln_mover_change!=.
		local beh_constant2 = string(round(_b[_cons],0.001),"%4.3f") 
				
	* creating plots
	local splitvar "dup"
				
	preserve
				
		expand 2, gen(dup)
						
		replace `behavior' = r_`behavior' if dup==1
		replace ln_own_change_w = r_ln_own_change_w if dup==1
				
		binscatter `behavior' ln_own_change_w if actual_ln_mover_change!=., by(`splitvar') savedata("$data/binscdata") replace reportreg
				
	restore
				
	preserve
	
		clear
					
		quietly do "$data/binscdata.do"
					
		quietly su ln_own_change_w_by1
		local xmin = r(min)
		local xmax = r(max)
					
		quietly su ln_own_change_w_by2
		if r(min)<`xmin' local xmin = r(min)
		if r(max)>`xmax' local xmax = r(max)
					
		local xmin = -0.3 
		local xmax = 0.8 
					
		if "`behavior'"!="negotiate_pct" & "`behavior'"!="negotiate_pct_c"{
			local ymin = 0
			local ymax = .6
		} 
		if "`behavior'"=="paycut_quit_pct" {
			local ymin = -0.1
			local ymax = 0.35
		}
		if "`behavior'"=="negotiate_pct_c" {
			local ymin = 0
			local ymax = 0.25
		}
										
		
		twoway scatter `behavior'_by1 ln_own_change_w_by1, mcolor(navy) msymbol(circle) ///
			|| scatter `behavior'_by2 ln_own_change_w_by2, mcolor(maroon) msymbol(Th) ///
			|| function y=(`beh_beta1'*x)+`beh_constant1', range(`xmin' `xmax') color(navy) ///
			|| function y=(`beh_beta2'*x)+`beh_constant2', range(`xmin' `xmax') color(maroon) lpattern(dash) /*
			*/ legend(order(1 "Uncontrolled, Slope: `beh_beta1' (SE `beh_se1')" 2 "Controlling for Objective Benchmark, Slope: `beh_beta2' (SE `beh_se2')") cols(1) ring(0) size(small) position(11) region(lwidth(none)))   ///
			ytitle(`ytitle') xtitle(`xtitle') ylabel(#10,grid) xlabel(#10,grid) xscale(range(`xmin' `xmax')) yscale(range(`ymin' `ymax'))
			
			
		if "`behavior'"=="switchout_proba" { // Figure VI a
			graph export ${log}/FigureVIa.pdf, replace
		}
		if "`behavior'"=="paycut_quit_pct" { // Figure VI b
			graph export ${log}/FigureVIb.pdf, replace
		}
		if "`behavior'"=="negotiate_proba" { // Figure VI c
			graph export ${log}/FigureVIc.pdf, replace

		}		
		if "`behavior'"=="negotiate_pct_c" { // Figure VI d
			graph export ${log}/FigureVId.pdf, replace

		}		
					
	restore
			
	cap erase "$data/binscdata.do" 
	cap erase "$data/binscdata.csv"
		
	drop r_`behavior' r_ln_own_change_w
				
}
									
					
		
		
cap log close
clear

